#!/usr/bin/env python
#coding=utf-8
from django.shortcuts import render_to_response,get_object_or_404
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from ishare.core.forms import ShareItemForm
from ishare.core.forms import PropertyForm
from ishare.core.forms import ItemImageForm
from django.forms.formsets import formset_factory
from ishare.core.models import ShareItem
from django.core.paginator import Paginator,InvalidPage, EmptyPage



def index(request):
    return render_to_response('core/index.html')

#=======查看分享物件==========

def items(request):
    """
    按更新时间查看分享物件的列表,
    """
    items_list = ShareItem.objects.filter(status=1).order_by('-add_time')
    paginator = Paginator(items_list,10)
    
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        items = paginator.page(page)
    except (EmptyPage, InvalidPage):
        items = paginator.page(paginator.num_pages)
    
    return render_to_response('core/items.html',{'items':items})

def item(request,id):
    """
    查看某一分享物细节.
    """
    item = get_object_or_404(ShareItem,id=id)
    # 加上评论
    
    return render_to_response('core/item.html',{'item':item})

#======发布分享物件===========

@login_required
def share(request):
    """
    分享一个物件,由方法是GET或POST判断是编辑页还是提交.
    """
    if request.method == "POST":
        form = ShareItemForm(request.POST,request.FILES)
        if form.is_valid():
            item = form.save(request)
            return HttpResponseRedirect('/item/%d/'%item.id)
        else:
            return render_to_response('core/share.html',{'form':form})
            
    else:
        form = ShareItemForm()
        return render_to_response('core/share.html',{'form':form})

#=====分类及标签=============

def categories(request):
    """
    分享物件分类地图
    """
    pass

def category(request,cate):
    """
    查看某一分类下的分享物品
    """
    pass

def tags(request):
    """
    分享物件标签
    """
    pass

def tag(request):
    """
    查看某一标签下的分享物品
    """
    pass
